package org.test.client.ui.component.paging;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;

import javax.swing.JButton;

import org.test.client.listener.ClickAction;
import org.test.client.listener.CommonMouseListener;
import org.test.client.ui.util.UIUtil;

/**
 * 暂时不支持 icon
 * @author Guodeqi
 *
 */
public class PreAndNextBtn extends JButton{
	private PreAndNextBtn thisBtn = this;
	private PagingBar parentBar = null;
	
	boolean start = true;
	String txt = null;
	
	int diameter = 16;
	
	//加 4 为了留一定间隙
	public int currentw = PagingBar.btnWidth + diameter + 4 ;
	public int currenth = PagingBar.btnHeight;
	
	boolean rollOvered = false;
	CommonMouseListener listener = null;
	Shape shape = null;
	
	public PreAndNextBtn(boolean start, String str) {
		this.start = start;
		this.txt = str;
		doInit();
	}

	public void doInit() {
		this.setOpaque(false);
		FontMetrics fm = this.getFontMetrics(this.getFont());
		int w = UIUtil.getStringWidth(txt, fm);
		if(w+diameter > currentw){
			currentw = w+diameter + 4;
		}
		
		this.setPreferredSize(new Dimension(currentw,currenth));
		this.setBorder(null);
		
		listener = new CommonMouseListener(thisBtn){
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!thisBtn.isSelected()){
					super.mouseEntered(e);
					setRollOvered(true);
				}
			}
			@Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				setRollOvered(false);
			}
		};
		listener.addClickAction(new ClickAction() {
			@Override
			public Object doOnClick(MouseEvent ev) {
				if(parentBar != null){
					if(start){
						parentBar.setCurrentPage(parentBar.getCurrentPage()-1);
					}else{
						parentBar.setCurrentPage(parentBar.getCurrentPage()+1);
					}
				}
				return null;
			}
		});
		this.addMouseListener(listener);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int w = this.getWidth();
		int h = this.getHeight();
		
		//back
		if(rollOvered){
			g2d.setColor(PagingBtn.rollOverColor);
		}else{
			g2d.setColor(PagingBtn.normalBackColor);
		}
		shape = createShape(w,h,start);
		g2d.fill(shape);

		//txt
		g2d.setColor(PagingBtn.fontColor);
		int x = w/2 - UIUtil.getTxtWidth(g2d, txt)/2;
		int y = h/2 + UIUtil.getTxty(g2d);
		g2d.drawString(txt, x, y);
		
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int w = this.getWidth();
		int h = this.getHeight();
		// border
		g2d.setColor(PagingBtn.borderColor);
		shape = createShape(w, h, start);
		g2d.draw(shape);
	}
	
	public Shape createShape(int w,int h,boolean start){
		GeneralPath shape = new GeneralPath();
		int radius = diameter /2 ;
		h = h-1;
		if(start){
			//XY是左上角的左边位置。W是宽H是高。 逆时针 画
			shape.append(new Arc2D.Double(0,0,diameter,diameter, 90,90,Arc2D.OPEN), true);
			shape.lineTo(0, h-radius);
			shape.append(new Arc2D.Double(0,h-diameter,diameter,diameter,180, 90,Arc2D.OPEN), true);
			shape.lineTo(w,h);
			shape.lineTo(w, 0);
			shape.lineTo(radius,0);
			
		}else{
			w= w-1;
			shape.append(new Arc2D.Double(w-diameter,h-diameter,diameter,diameter, 270,90,Arc2D.OPEN), true);
			shape.lineTo(w, h-radius);
			shape.append(new Arc2D.Double(w-diameter,0,diameter,diameter,0, 90,Arc2D.OPEN), true);
			shape.lineTo(0, 0);
			shape.lineTo(0,h);
			shape.lineTo(w-radius,h);
		}
		
		shape.closePath();
		return shape;
	}
	
	public void setRollOvered(boolean flag){
		if(flag == rollOvered){
			return;
		}
		rollOvered = flag;
		thisBtn.repaint();
	}

	public void setParentBar(PagingBar parentBar) {
		this.parentBar = parentBar;
	}
	
	public void setCurrentH(int h ){
		currenth = h;
		this.setPreferredSize(new Dimension(this.getPreferredSize().width,currenth));
	}
	
}
